package socket;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

//客户端
public class Client {
    private Socket socket; //套接字
    //构造器15050691858
    public Client(){
        try {
            System.out.println("正在连接服务器...");
//            socket = new Socket("176.15.18.145",8088);
            socket = new Socket("localhost",8088);
//            socket = new Socket("176.15.18.168",8088);
            System.out.println("与服务器端建立连接!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        //将读取来自服务端消息的线程启动起来
        ServerHandler serverHandler = new ServerHandler();
        Thread t = new Thread(serverHandler);
        t.setDaemon(true);
        t.start();

        //客户端向服务端发送数据,则需要使用socket获取输出流
        try {
            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw,true);

            //pw.println("你好啊，蔡运豪大傻子，嘿嘿嘿！");
            Scanner scanner = new Scanner(System.in);
		    System.out.println("请开始输入内容:");
		    while(true) {
		        String line = scanner.nextLine();
                if("exit".equals(line)) {
                   break;
                }
                pw.println(line);
		    }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }

    private class ServerHandler implements Runnable{
        public void run(){
            //通过socket获取输入流，用于读取服务端发过来的消息
            try {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);

                String line;
                while ((line = br.readLine()) != null){
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


//    FileOutputStream fos = new FileOutputStream("pw.txt");
//    OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
//    BufferedWriter bw = new BufferedWriter(osw);
//    PrintWriter pw = new PrintWriter(bw,true);
//
//    Scanner scanner = new Scanner(System.in);
//		System.out.println("请开始输入内容");
//                while(true) {
//                String str = scanner.nextLine();
//                if("exit".equals(str)) {
//                break;
//                }
//                pw.println(str);
//                }
//                System.out.println("写出完毕！");
//                pw.close();